package com.core.shared;

import com.core.dto.SocketUserDTO;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-1-9
 * Time: 上午11:44
 * To change this template use File | Settings | File Templates.
 */
public class DataToJson {

    private static ObjectMapper mapper = new ObjectMapper();

    private static void DataToJson(String content, HttpServletResponse response) {
        try {
            PrintWriter out = response.getWriter();
            String json = mapper.writeValueAsString(content);
            out.print(json);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void socketUserToJson(List<SocketUserDTO> socketUserDTOs, HttpServletResponse response) {
        StringBuilder s = new StringBuilder();
        for (SocketUserDTO socketUserDTO : socketUserDTOs) {
            s.append("<label><input type=radio name=socket_users value=").append(socketUserDTO.getSessionId()).append(">").append(socketUserDTO.getUsername()).append("</label>");
        }
        DataToJson(s.toString(), response);
    }

}
